
class EventDispatcher :
    def __init__ (self,owner): 
        self._owner = owner
        self._listener = {}
    
    def addListener (self , type, callback) :
        if not self._listener.has_key( type ) :
            self._listener[type] = []
        self._listener[type].append( callback )
        
    def removeListener (self , type, callback) :
        if self._listener.has_key( type ) :
            self._listener[type].remove( callback )
            
    def dispatch(self, type, evt) :
        evt.owner = self._owner
        evt.type = type

        if self._listener.has_key( type ) :
            for e in self._listener[type] :
                try :
                    apply( e, [evt] )
                except Exception ,e:
                    print e
                if evt.cancel : return False
        return True
    
    
class Event :
    def __init__(self,args = {} ) :
        self.cancel = False
        self.owner = None
        self.args = args
        self.type = 'none'